﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using log4net;

namespace Ewts.Storage
{
    public partial class BarCodeWRL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int barCodeID = 0;
                if (Request["barCodeID"] != null && Request["barCodeID"].ToString().Trim() != "")
                {
                    try
                    {
                        barCodeID = int.Parse(Request["barCodeID"].ToString());
                    }
                    catch
                    {
                        
                        DataLib.Common.MessageBox.Show(Page,"非法链接");
                    }
                }

                DataLib.BLL.BarCode bBll = new DataLib.BLL.BarCode();
                DataLib.Model.BarCode bModel = bBll.GetModel(barCodeID);
                DataLib.Model.BarCode barCodeM = bBll.GetByValue(bModel.barcodevalue);
                if (barCodeM !=null)
                {
                    //移库
                    if(barCodeM.ParmentID!=null && barCodeM.ParmentID !=0)
                    {
                        DataLib.Model.OutStorage oModel = new DataLib.BLL.OutStorage().GetModel((int)barCodeM.ParmentID);
                         string cumname = "";
                         if (oModel.VLoadNum.Substring(0, 2) == "86")
                         {
                             DataLib.Model.LoadNum lmodel = new DataLib.BLL.LoadNum().GetLoadID(oModel.VLoadNum);
                             if (lmodel.labname != null && lmodel.labname.ToString().Trim() != "")
                                 cumname = lmodel.labname;
                             else
                                 cumname = lmodel.cumname;
                         }
                         else 
                         {
                             DataLib.BLL.Vload vload = new DataLib.BLL.Vload();
                             DataLib.Model.Vload mvload = vload.GetModelByOutID((int)oModel.id);
                             cumname = mvload.inscumname;
                         }
                         lblNo.InnerHtml = "Y01";
                        lblCustomerName.InnerText = cumname;
                        lblcutomer.InnerText = cumname;
                    }
                    else if (barCodeM.loadnumid != null && barCodeM.loadnumid != 0)
                    {
                        DataLib.Model.LoadNum loadModel = new DataLib.BLL.LoadNum().GetModel((int)barCodeM.loadnumid);
                      //  DataLib.Model.Cumtomer cuModel = new DataLib.BLL.Cumtomer().GetModel((int)loadModel.customerid);
                        if (loadModel.labname != null && loadModel.labname.ToString().Trim() != "")
                        {
                            lblCustomerName.InnerText = loadModel.labname;
                            lblcutomer.InnerText = loadModel.labname;
                        }
                        else
                        {
                            lblCustomerName.InnerText = loadModel.cumname;
                            lblcutomer.InnerText = loadModel.cumname;
                        }
                        if (barCodeM.cjoid != null && barCodeM.cjoid != 0) {
                            DataLib.Model.Cjo c = new DataLib.Model.Cjo();
                            DataLib.BLL.Cjo b = new DataLib.BLL.Cjo();
                            c = b.GetModel(barCodeM.cjoid);
                            if (c != null)
                            {
                                lblNo.InnerHtml = c.cjono.Substring(c.cjono.Length - 3, 3);
                            }
                        }
                    }
                    else 
                    {
                        DataLib.Common.MessageBox.Show(Page,"数据异常，请联系管理员 ");
                    }
                }
                DataLib.Model.StoragePostion storPosM =new DataLib.Model.StoragePostion();
                if (barCodeM != null && barCodeM.storageid != null)
                {
                    storPosM = new DataLib.BLL.StoragePostion().GetModel(int.Parse(barCodeM.storageid.ToString()));
                }

                if (storPosM != null)
                { 
                }
                
                if ( barCodeM!=null)
                {
                    lblLoadNum.InnerText = barCodeM.loadnumv;
                    if (barCodeM.cjoid != 0)
                    {

                        DataTable dt = new DataLib.BLL.VehicleRequire().GetList("cjoid=" + barCodeM.cjoid.ToString()).Tables[0];
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            DataLib.Model.VehicleRequire vrModel = new DataLib.BLL.VehicleRequire().GetModel(int.Parse(dt.Rows[0]["id"].ToString().Trim()));
                            lblSpecial.InnerText = vrModel.peculiarrequire;
                        }
                    }



                    lblDate.InnerText = ((DateTime)bModel.createdate).ToShortDateString();
 
                    lblGoodsCode.InnerText = barCodeM.goodscode;
                    lblGoodsName.InnerText = barCodeM.goodsname;
                    lblGwt.InnerText = barCodeM.gwt.ToString();
                    lblTare1.InnerText = barCodeM.tare1.ToString();
                    lblNwt.InnerText = barCodeM.nwt.ToString();
                    lblTare2.InnerText = barCodeM.tare2.ToString();
                    lblQty.InnerText = barCodeM.qty.ToString();
                    lblRemark.InnerText = barCodeM.remark;
                    lblBarCode.InnerText = GetTrimBarcode(barCodeM.barcodevalue.ToUpper());
                    lblBarCode2.InnerText = GetTrimBarcode(barCodeM.barcodevalue.ToUpper());
                     lblnwt_b.InnerText = barCodeM.nwt.ToString();
                    lblxl_b.InnerText =  barCodeM.packageno.ToString();
                }

                img1.ImageUrl = "/ashx/BarCodePrint.ashx?code=" + barCodeM.barcodevalue.ToUpper() + "&height=30";
                img2.ImageUrl = "/ashx/BarCodePrint.ashx?code=" + barCodeM.barcodevalue.ToUpper() + "&height=30";

            }

        }


        /// <summary>
        /// 设置空格的Barcode
        /// </summary>
        /// <param name="barcode"></param>
        /// <returns></returns>
        public string GetTrimBarcode(string barcode)
        {
            string str = barcode;
            if (str.Length >= 30)
                str = str.Insert(27, " ");
            if (str.Length >= 17)
                str = str.Insert(17, " ");
            if (str.Length >= 9)
                str = str.Insert(9, " ");
            return str;
        }
    }
}